<?php


namespace app\sports\jc\service;

use app\sports\jc\model\JcLinkMode;
use think\Exception;
use app\common\HTTP;
use think\facade\Log;
use app\common\InstanceTrait;

class JcLinkCaiji
{

     private $user = '';
    private  $secret ='';
//    接口名:体彩比赛关联
//    接口频次:3-5分钟
    private $url = 'open.sportnanoapi.com/api/v2/sports/lottery/match/list';

    protected function __construct()
    {
        $this->user = Env::get('nami.user');
        $this->secret = Env::get('nami.secret');
    }
    /*
     * 体彩比赛关联采集
     */
    public function Execs(){
        try{
            $jcressult = json_decode(HTTP::getInstance()->doGet($this->url."?user=$this->user&secret=$this->secret"), true);

             $jcapiid = [];
             foreach ($jcressult as $value){
    //             取得接口返回的纳米比赛id
                array_push($value['match_id']);
             }
    //         查询数据库
            $modeldata=JcLinkMode::getInstance()->QueryIn($jcapiid);
             $dataall = [];
             foreach ($modeldata as $value) {
                 foreach ($jcressult as $v){
                     if ($value['nami_id'] == $v['id']){
    //                     存在的及更新
                         $data=[
                            'sport_id'   =>$v['sport_id'],
                             'match_id'  =>$v['match_id'],
                             'lottery_type' => $v['lottery_type'],
                             'nami_id'     => $v['id'],
                             'update_time' => date('Y-m-d H:i:s',time()),
                         ];
                        JcLinkMode::getInstance()->UpdateJc($v['id'],$data);
                     }else{
    //                     不存在添加
                         $dataall[]=[
                             'sport_id'     => $v['sport_id'],
                             'match_id'     => $v['match_id'],
                             'lottery_type' => $v['lottery_type'],
                             'nami_id'      => $v['id'],
                             'update_time'  => date('Y-m-d H:i:s',time()),
                         ];
                     }
                 }
             }
             if (isset($dataall) && (!empty($dataall))){
                 JcLinkMode::getInstance()->InsertJcLinkAll($dataall);
             }
             Log::log('info','体彩比赛关联采集成功');
        }catch(Exception $e){
            Log::error('足球竞彩采集错误:'.$e->getMessage());
            return [];
        }
    }

}